#ifndef __ENGINE_V8__
#define __ENGINE_V8__
#ifdef WIN32
#pragma warning (disable : 4251)
#endif
#include <node.h>
#include <node_object_wrap.h>
#include "engine.h"

namespace ccengin {

class EngineWrapper : public node::ObjectWrap {
public:
	static void init(v8::Isolate* isolate);
	static void newInstance(const v8::FunctionCallbackInfo<v8::Value> &args);
	inline bool findSolutionDirect(float seconds, int& x1, int& y1, int& x2, int& y2);
	inline void findOver();
private:
  EngineWrapper();
  ~EngineWrapper();

	static void construct(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void startup(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void move(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void undo(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void getMoveCount(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void getPlayer(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void getState(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void findSolutionSync(const v8::FunctionCallbackInfo<v8::Value> &args);
	static void findSolution(const v8::FunctionCallbackInfo<v8::Value> &args);
  static void getLastMove(const v8::FunctionCallbackInfo<v8::Value> &args);
  static v8::Persistent<v8::Function> constructor;

private:
	chess::Engine engine;
	volatile bool finding = false;
};

inline bool EngineWrapper::findSolutionDirect(float seconds, int& x1, int& y1, int& x2, int& y2) {
	bool result = this->engine.findSolution(seconds, x1, y1, x2, y2);
	return result;
}

inline void EngineWrapper::findOver() {
	this->finding = false;
}

} // namespace ccengin

#endif //__ENGINE_V8__
